<template>
  <div>
    <van-nav-bar left-arrow title="编辑资料" @click-left="$router.back()" />
    <div class="image-box">
      <van-image fit="cover" round :src="userInfo.photo" />
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group>
      <van-cell
        @click="clickNameCell"
        title="名称"
        :value="userInfo.name"
        is-link
      />
      <van-cell
        @click="editGender = true"
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        is-link
      />
      <van-cell
        @click="clickBirthCell"
        title="生日"
        :value="userInfo.birthday"
        is-link
      />
    </van-cell-group>
    <div v-show="cropShow" class="crop-box">
      <vueCropper
        ref="cropper"
        :img="cropImg"
        autoCrop
        autoCropWidth="200"
        autoCropHeight="200"
      ></vueCropper>
      <van-button style="left: 0" type="primary" size="small" @click="doCrop"
        >裁剪</van-button
      >
      <van-button
        style="right: 0"
        type="primary"
        size="small"
        @click="cropShow = false"
        >取消</van-button
      >
    </div>
    <van-dialog
      @confirm="changeName"
      v-model="showName"
      title="输入昵称"
      show-cancel-button
    >
      <van-field
        v-model="editName"
        ref="inp"
        style="border: 1px solid #ddd"
        placeholder="请输入昵称"
      />
    </van-dialog>
    <van-popup
      v-model="changeBirthday"
      position="bottom"
      :style="{ height: '30%' }"
    >
      <van-datetime-picker
        @cancel="changeBirthday = false"
        @confirm="confirmChange"
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
      />
    </van-popup>
    <!-- 修改性别弹出层 -->
    <van-popup v-model="editGender" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-text="取消"
        @click-left="editGender = false"
      />
      <van-cell @click="changeGender(0)" title="男" is-link />
      <van-cell @click="changeGender(1)" title="女" is-link />
    </van-popup>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { VueCropper } from 'vue-cropper'
import { editPhotoAPI, editUserInfoAPI } from '@/api/user'
import dayjs from 'dayjs'
export default {
  components: {
    VueCropper
  },
  data () {
    return {
      cropImg: '',
      cropShow: false,
      showName: false,
      editGender: false,
      editName: '',
      changeBirthday: false,
      minDate: new Date(1949, 0, 1),
      maxDate: new Date(),
      currentDate: new Date()
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  created () {
    this.$store.dispatch('user/reqSetInfo')
  },
  methods: {
    // 修改性别
    async changeGender (gender) {
      await editUserInfoAPI({
        gender
      })
      // 修改到vuex里
      this.$store.commit('user/setUserInfo', {
        ...this.userInfo,
        gender
      })
      this.editGender = false
    },

    async confirmChange () {
      this.changeBirthday = false
      const birthday = dayjs(this.currentDate).format('YYYY-MM-DD')
      await editUserInfoAPI({
        birthday
      })
      // 修改到vuex里
      this.$store.commit('user/setUserInfo', {
        ...this.userInfo,
        birthday
      })
    },
    clickBirthCell () {
      this.changeBirthday = true
      this.currentDate = new Date(this.userInfo.birthday)
    },
    async clickNameCell () {
      this.showName = true
      this.editName = this.userInfo.name
      await this.$nextTick()
      this.$refs.inp.focus()
    },
    // 点击了名字的确定修改按钮
    async changeName () {
      // 非空判断
      if (this.editName === '') return this.$toast('昵称不能为空')
      // 不为空就发请求
      await editUserInfoAPI({
        name: this.editName
      })

      // 修改到vuex里
      this.$store.commit('user/setUserInfo', {
        ...this.userInfo,
        name: this.editName
      })
    },
    afterRead (file) {
      this.cropImg = file.content
      this.cropShow = true
      // 此时可以自行将文件上传至服务器
      //   console.log(file)
    },
    doCrop () {
      this.$refs.cropper.getCropBlob(async data => {
        // 点击头像上传并阅读完所选定文件后
        const fd = new FormData()
        fd.append('photo', data)
        const res = await editPhotoAPI(fd)
        this.$store.commit('user/setUserInfo', {
          ...this.userInfo,
          photo: res.data.photo
        })
        this.cropShow = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
.image-box {
  height: 160px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  .van-image {
    height: 130px;
    width: 130px;
  }
  .van-uploader {
    width: 100%;
    height: 100%;
    position: absolute;
    opacity: 0;
    ::v-deep .van-uploader__wrapper {
      width: 100%;
      height: 100%;
      .van-uploader__upload {
        width: 100%;
        height: 100%;
      }
    }
  }
}
.crop-box {
  position: fixed;
  width: 100vw;
  height: 100vh;
  left: 0;
  top: 0;
  .van-button {
    position: absolute;
    bottom: 0;
  }
}
</style>
